import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class monopolyGUI implements ActionListener, Runnable{
	private JFrame frame;
	private JPanel panel;
	private JLabel labelMonopoly, labelTurn1, labelTurn2, JailButton,DiceLabel1,DiceLabel2,Pimage;
	private JButton buttonBail, buttonRoll,buttonBuild,buttonSell,buttonMortgage,rollButton,endTurn,buttonBuy,buttonPay,buttonGo,buttonOk,testButton1,testButton2,start,buttonTrade,buttonAuction,buttonBet;
	private ImageIcon iiFullBoard,iiDice1,iiDice2,iiDice3,iiDice4,iiDice5,iiDice6,build,sell,mortgage;
	private Dice Dice1, Dice2;
	private int nCounter1,nCounter2,Move,temp1,temp2,turn=0,jailCounter=0,diceValue1=1,diceValue2=1;
	private boolean isMove=false,isRolling=false,isForward=false,temp=false,isBackward=false,isToJail=false,isPair=false,isRoll=false;
	private Thread t;
	private Random generate = new Random();
	private Integer[] orderTurn;
	private Player[] player;
	private SpecialTiles checker;
	private Board properties;
	private ImageIcon[] deeds, Block;
	private JButton[] Build,Sell,Mortgage,Deeds;
	private JLabel[] House;

	
	
	public monopolyGUI(Player[] player, Board properties){
		this.properties = properties;
		this.player = player;
		generateOrder(this.player.length);
	}
	
	public void startThread(){
		t = new Thread(this);
		t.start();
	}

	public void setButtons(){
		rollButton.setEnabled(false);
		endTurn.setEnabled(true);
	}
	
	public void setUI(ImageIcon ii, boolean isMove, int Move){
		if(isMove){
			setForward(true);
		}
		else{
			setBackward(true);
		}
		this.Move = Move;
		JailButton.setVisible(true);
		JailButton.setIcon(new ImageIcon("images/Scroll/scrollnew.png"));
		JailButton.setIcon(ii);
		buttonOk.setVisible(true);
	}
	
	public void setUI(ImageIcon ii){
		JailButton.setVisible(true);
		JailButton.setIcon(new ImageIcon("images/Scroll/scrollnew.png"));
		JailButton.setIcon(ii);
		buttonOk.setVisible(true);
	}
	
	public void removeUI(){
		JailButton.setVisible(false);
		buttonOk.setVisible(false);
	}
	
	public void generateOrder(int players){
		orderTurn = new Integer[players];
		int temp, counter = 0;
		for (int i = 0; i < orderTurn.length; i++) {
			orderTurn[i] = 50;
		}
		for (int i = 0; i < orderTurn.length; i++) {
			temp = generate.nextInt(orderTurn.length);
			counter=0;
			for (int k = 0; k < orderTurn.length; k++) {
				if(temp == orderTurn[k]){
					counter++;
				}
			}
			if(counter == 0){
				orderTurn[i] = temp;
			}
			else
				i--;
		}
	}
	
	public void createLabelJail(){
		JailButton.setIcon(new ImageIcon("images/Scroll/scrollnew.png"));
		buttonRoll.setVisible(true);
		buttonBail.setVisible(true);
		JailButton.setVisible(true);
	}
	
	public void removeLabelJail(){
		buttonRoll.setVisible(false);
		buttonBail.setVisible(false);
		JailButton.setVisible(false);
	}
	
	public void createLabelBuy(){
		JailButton.setIcon(new ImageIcon("images/Scroll/scrollnew.png"));
		buttonBuy.setVisible(true);
		JailButton.setVisible(true);
	}
	
	public void removeLabelBuy(){
		buttonBuy.setVisible(false);
		JailButton.setVisible(false);
	}
	
	public void createLabelPay(){
		JailButton.setIcon(new ImageIcon("images/Scroll/scrollnew.png"));
		buttonPay.setVisible(true);
		JailButton.setVisible(true);
	}
	
	public void removeLabelPay(){
		buttonPay.setVisible(false);
		JailButton.setVisible(false);
	}

	public void createLabelGoToJail() {
		JailButton.setIcon(new ImageIcon("images/Scroll/scrollnew.png"));
		buttonGo.setVisible(true);
		JailButton.setVisible(true);
	}
	
	public void removeLabelGoToJail() {
		buttonGo.setVisible(false);
		JailButton.setVisible(false);
	}
	
	public void initComponents(){
		frame = new JFrame("Monopoly");
		panel = new JPanel();
		iiDice1 = new ImageIcon("images/dice/1.png");
		iiDice2 = new ImageIcon("images/dice/2.png");
		iiDice3 = new ImageIcon("images/dice/3.png");
		iiDice4 = new ImageIcon("images/dice/4.png");
		iiDice5 = new ImageIcon("images/dice/5.png");
		iiDice6 = new ImageIcon("images/dice/6.png");
		DiceLabel1 = new JLabel(iiDice1);
		DiceLabel2 = new JLabel(iiDice1);
		JailButton = new JLabel();
		start = new JButton("Start");
		testButton1 = new JButton();
		testButton2 = new JButton();
		buttonBuild = new JButton("Build House");
		buttonRoll = new JButton("Roll for Doubles");
		buttonBail = new JButton("Post Bail");
		buttonBuy = new JButton("Buy");
		buttonPay = new JButton("Pay");
		buttonTrade =new JButton("Trade");
		buttonAuction =new JButton("Auction");
		buttonBet =new JButton("Bet");
		buttonSell = new JButton("Sell House");
		buttonMortgage =new JButton("Mortgage");
		buttonGo = new JButton("Go to Jail");
		rollButton = new JButton("ROLL");
		endTurn = new JButton("End Turn");
		buttonOk = new JButton("OK");
		iiFullBoard = new ImageIcon("images/thefinalboard.jpg");
		Block = new ImageIcon[40];
		Pimage = new JLabel();
		int counter=0;
		for (int i = 0; i < 39; i++) {
			Block[i] = new ImageIcon("images/stat/"+counter+".png");
			counter++;
		}
		Deeds = new JButton[28];
		deeds = new ImageIcon[28];
		int c=0;

		for (int i = 0; i < 28; i++) {
			deeds[i] = new ImageIcon("images/Scroll/"+c+".png");
			c++;
		}
		   c=0;
		for (int i = 0; i < 28; i++) {
			Deeds[i] = new JButton(deeds[i]);
			c++;
		}
		
		for(int i = 0; i < orderTurn.length; i++){
			player[orderTurn[i]].getToken().createLabel();
		}
		build = new ImageIcon("images/button/build.png");		
		sell = new ImageIcon("images/button/sell.png");		
		mortgage = new ImageIcon("images/button/mortgage.png");
		
		Mortgage = new JButton[22];
		for (int j = 0; j < 22; j++) {
			Mortgage[j] = new JButton("");
			Mortgage[j].setIcon(mortgage);
			Mortgage[j].setBorder(null);
			Mortgage[j].setBorderPainted(false);
			Mortgage[j].setContentAreaFilled(false);
			Mortgage[j].setOpaque(false);
			Mortgage[j].setRolloverIcon(new ImageIcon("images/button/mortgage1.png"));
			Mortgage[j].addActionListener(this);
			Mortgage[j].setActionCommand("motage"+j);
		}
		
		Mortgage[0].setBounds(554, 665, 55, 30);
		Mortgage[1].setBounds(439, 665, 55, 30);
		Mortgage[2].setBounds(268, 665, 55, 30);
		Mortgage[3].setBounds(153, 665, 55, 30);
		Mortgage[4].setBounds(96, 665, 55, 30);
		
		Mortgage[5].setBounds(5, 554, 55, 30);
		Mortgage[6].setBounds(5, 439, 55, 30);
		Mortgage[7].setBounds(5, 382, 55, 30);
		Mortgage[8].setBounds(5, 267, 55, 30);
		Mortgage[9].setBounds(5, 152, 55, 30);
		Mortgage[10].setBounds(5, 95, 55, 30);
		
		Mortgage[11].setBounds(96, 5, 55, 30);
		Mortgage[12].setBounds(211, 5, 55, 30);
		Mortgage[13].setBounds(268, 5, 55, 30);
		Mortgage[14].setBounds(383, 5, 55, 30);
		Mortgage[15].setBounds(440, 5, 55, 30);
		Mortgage[16].setBounds(555, 5, 55, 30);
		
		Mortgage[17].setBounds(648, 96, 55, 30);
		Mortgage[18].setBounds(648, 153, 55, 30);
		Mortgage[19].setBounds(648, 268, 55, 30);
		Mortgage[20].setBounds(648, 439, 55, 30);
		Mortgage[21].setBounds(648, 554, 55, 30);
		
		for (int i = 0; i < 22; i++) {
			Mortgage[i].setVisible(false);
		}
		
		panel.add(Mortgage[0]);
		panel.add(Mortgage[1]);
		panel.add(Mortgage[2]);
		panel.add(Mortgage[3]);
		panel.add(Mortgage[4]);
		panel.add(Mortgage[5]);
		panel.add(Mortgage[6]);
		panel.add(Mortgage[7]);
		panel.add(Mortgage[8]);
		panel.add(Mortgage[9]);
		panel.add(Mortgage[10]);
		panel.add(Mortgage[11]);
		panel.add(Mortgage[12]);
		panel.add(Mortgage[13]);
		panel.add(Mortgage[14]);
		panel.add(Mortgage[15]);
		panel.add(Mortgage[16]);
		panel.add(Mortgage[17]);
		panel.add(Mortgage[18]);
		panel.add(Mortgage[19]);
		panel.add(Mortgage[20]);
		panel.add(Mortgage[21]);
		
		Build = new JButton[22];
		for (int j = 0; j < 22; j++) {
			Build[j] = new JButton(build);
			Build[j].setBorder(null);
			Build[j].setBorderPainted(false);
			Build[j].setContentAreaFilled(false);
			Build[j].setOpaque(false);
			Build[j].setRolloverIcon(new ImageIcon("images/button/build1.png"));
			Build[j].addActionListener(this);
			Build[j].setActionCommand("build"+j);
		}

		Build[0].setBounds(554, 665, 55, 30);
		Build[1].setBounds(439, 665, 55, 30);
		Build[2].setBounds(268, 665, 55, 30);
		Build[3].setBounds(153, 665, 55, 30);
		Build[4].setBounds(96, 665, 55, 30);
		
		Build[5].setBounds(5, 554, 55, 30);
		Build[6].setBounds(5, 439, 55, 30);
		Build[7].setBounds(5, 382, 55, 30);
		Build[8].setBounds(5, 267, 55, 30);
		Build[9].setBounds(5, 152, 55, 30);
		Build[10].setBounds(5, 95, 55, 30);
		
		Build[11].setBounds(96, 5, 55, 30);
		Build[12].setBounds(211, 5, 55, 30);
		Build[13].setBounds(268, 5, 55, 30);
		Build[14].setBounds(383, 5, 55, 30);
		Build[15].setBounds(440, 5, 55, 30);
		Build[16].setBounds(555, 5, 55, 30);
		
		Build[17].setBounds(648, 96, 55, 30);
		Build[18].setBounds(648, 153, 55, 30);
		Build[19].setBounds(648, 268, 55, 30);
		Build[20].setBounds(648, 439, 55, 30);
		Build[21].setBounds(648, 554, 55, 30);
		
		for (int i = 0; i < 22; i++) {
			Build[i].setVisible(false);
		}
		
		panel.add(Build[0]);
		panel.add(Build[1]);
		panel.add(Build[2]);
		panel.add(Build[3]);
		panel.add(Build[4]);
		panel.add(Build[5]);
		panel.add(Build[6]);
		panel.add(Build[7]);
		panel.add(Build[8]);
		panel.add(Build[9]);
		panel.add(Build[10]);
		panel.add(Build[11]);
		panel.add(Build[12]);
		panel.add(Build[13]);
		panel.add(Build[14]);
		panel.add(Build[15]);
		panel.add(Build[16]);
		panel.add(Build[17]);
		panel.add(Build[18]);
		panel.add(Build[19]);
		panel.add(Build[20]);
		panel.add(Build[21]);
		
		Sell = new JButton[22];
		for (int i = 0; i < 22; i++) {
			Sell[i] = new JButton(sell);

			Sell[i] = new JButton(sell);
			Sell[i].setBorder(null);
			Sell[i].setBorderPainted(false);
			Sell[i].setContentAreaFilled(false);
			Sell[i].setOpaque(false);
			Sell[i].setRolloverIcon(new ImageIcon("images/button/sell1.png"));
			Sell[i].addActionListener(this);
			Sell[i].setActionCommand("sell"+i);
		
		}
		Sell[0].setBounds(554, 665, 55, 30);
		Sell[1].setBounds(439, 665, 55, 30);
		Sell[2].setBounds(268, 665, 55, 30);
		Sell[3].setBounds(153, 665, 55, 30);
		Sell[4].setBounds(96, 665, 55, 30);
		
		Sell[5].setBounds(5, 554, 55, 30);
		Sell[6].setBounds(5, 439, 55, 30);
		Sell[7].setBounds(5, 382, 55, 30);
		Sell[8].setBounds(5, 267, 55, 30);
		Sell[9].setBounds(5, 152, 55, 30);
		Sell[10].setBounds(5, 95, 55, 30);
		
		Sell[11].setBounds(96, 5, 55, 30);
		Sell[12].setBounds(211, 5, 55, 30);
		Sell[13].setBounds(268, 5, 55, 30);
		Sell[14].setBounds(383, 5, 55, 30);
		Sell[15].setBounds(440, 5, 55, 30);
		Sell[16].setBounds(555, 5, 55, 30);
		
		Sell[17].setBounds(648, 96, 55, 30);
		Sell[18].setBounds(648, 153, 55, 30);
		Sell[19].setBounds(648, 268, 55, 30);
		Sell[20].setBounds(648, 439, 55, 30);
		Sell[21].setBounds(648, 554, 55, 30);
		
		for (int i = 0; i < 22; i++) {
			Sell[i].setVisible(false);
		}
		
		panel.add(Sell[0]);
		panel.add(Sell[1]);
		panel.add(Sell[2]);
		panel.add(Sell[3]);
		panel.add(Sell[4]);
		panel.add(Sell[5]);
		panel.add(Sell[6]);
		panel.add(Sell[7]);
		panel.add(Sell[8]);
		panel.add(Sell[9]);
		panel.add(Sell[10]);
		panel.add(Sell[11]);
		panel.add(Sell[12]);
		panel.add(Sell[13]);
		panel.add(Sell[14]);
		panel.add(Sell[15]);
		panel.add(Sell[16]);
		panel.add(Sell[17]);
		panel.add(Sell[18]);
		panel.add(Sell[19]);
		panel.add(Sell[20]);
		panel.add(Sell[21]);


		House = new JLabel[22];
		for (int h = 0; h < 22; h++) {
			House[h] = new JLabel();
		}

		House[0].setBounds(554, 610, 55, 30);
		House[1].setBounds(439, 610, 55, 30);
		House[2].setBounds(268, 610, 55, 30);
		House[3].setBounds(153, 610, 55, 30);
		House[4].setBounds(96, 610, 55, 30);
		
		House[5].setBounds(75, 554, 30, 55);
		House[6].setBounds(75, 439, 30, 55);
		House[7].setBounds(75, 382, 30, 55);
		House[8].setBounds(75, 267, 30, 55);
		House[9].setBounds(75, 152, 30, 55);
		House[10].setBounds(75, 95, 30, 55);
		
		House[11].setBounds(96, 75, 55, 30);
		House[12].setBounds(211, 75, 55, 30);
		House[13].setBounds(268, 75, 55, 30);
		House[14].setBounds(383, 75, 55, 30);
		House[15].setBounds(440, 75, 55, 30);
		House[16].setBounds(555, 75, 55, 30);
		
		House[17].setBounds(610, 96, 30, 55);
		House[18].setBounds(610, 153, 30, 55);
		House[19].setBounds(610, 268, 30, 55);
		House[20].setBounds(610, 439, 30, 55);
		House[21].setBounds(610, 554, 30, 55);
		
		panel.add(House[0]);
		panel.add(House[1]);
		panel.add(House[2]);
		panel.add(House[3]);
		panel.add(House[4]);
		panel.add(House[5]);
		panel.add(House[6]);
		panel.add(House[7]);
		panel.add(House[8]);
		panel.add(House[9]);
		panel.add(House[10]);
		panel.add(House[11]);
		panel.add(House[12]);
		panel.add(House[13]);
		panel.add(House[14]);
		panel.add(House[15]);
		panel.add(House[16]);
		panel.add(House[17]);
		panel.add(House[18]);
		panel.add(House[19]);
		panel.add(House[20]);
		panel.add(House[21]);
	
		labelMonopoly = new JLabel(iiFullBoard);
		labelTurn1 = new JLabel();
		labelTurn2 = new JLabel();
		labelTurn2.setFont(new Font("sansserif", Font.BOLD, 32));
		labelTurn2.setForeground(Color.blue);
		Dice1 = new Dice();
		Dice2 = new Dice();
		checker = new SpecialTiles();
		panel.setLayout(null);
		DiceLabel1.setBounds(800,650,50,50);
		DiceLabel2.setBounds(850,650,50,50);
		testButton1.setBounds(850,650,50,50);
		testButton2.setBounds(800,650,50,50);
		rollButton.setBounds(700,650,80,30);
		endTurn.setBounds(900,650,80,30);
		labelMonopoly.setBounds(0,0,1000,700);
		labelTurn1.setBounds(700, 100, 300, 300);
		labelTurn2.setBounds(800, 525, 250, 30);
		JailButton.setBounds(200, 240, 300, 300);
		buttonSell.setBounds(800,560,100,30);
		buttonBuild.setBounds(700, 560, 100, 30);
		buttonMortgage.setBounds(900, 560, 100, 30);
		buttonBail.setBounds(230, 380, 100, 30);
		buttonGo.setBounds(700, 550, 100, 30);
		buttonBuy.setBounds(700, 620, 100, 30);
		buttonPay.setBounds(700, 620, 100, 30);
		buttonTrade.setBounds(700, 590, 100, 30);
		buttonAuction.setBounds(800, 590, 100, 30);
		buttonBet.setBounds(900, 590, 100, 30);
		buttonOk.setBounds(800, 620, 100, 30);
		buttonRoll.setBounds(340, 380, 130, 30);
		int ix=815,iy=400;
		for (int i = 0; i < 28; i++) {
			if(ix<=980&&iy==400){
				Deeds[i].setBounds(ix, iy, 22, 40);
				if(i==1){
					ix+=25;
				}
				ix+=25;
				if (ix >= 975){
					ix=715;
					iy=440;
				}
			}else if(ix<=980&&iy==440){
				Deeds[i].setBounds(ix, iy, 22, 40);
				ix+=25;
				if (ix >= 980){
					ix=715;
					iy=480;
				}
			}else if(ix<=980&&iy==480){
				Deeds[i].setBounds(ix, iy, 22, 40);
				ix+=25;
			}
		}
		Pimage.setBounds(930, 10, 30, 40);
		start.setBounds(300, 350, 100, 100);
		start.addActionListener(this);
		start.setActionCommand("start");
		buttonSell.setEnabled(true);
		buttonSell.addActionListener(this);
		buttonSell.setActionCommand("Sell");
		buttonBuild.setEnabled(true);
		buttonBuild.addActionListener(this);
		buttonBuild.setActionCommand("build");
		testButton1.setEnabled(false);
		testButton2.setEnabled(false);
		rollButton.setEnabled(false);
		testButton1.setBorder(null);
		testButton1.setBorderPainted(false);
		testButton1.setContentAreaFilled(false);
		testButton1.setOpaque(false);
		testButton2.setBorder(null);
		testButton2.setBorderPainted(false);
		testButton2.setContentAreaFilled(false);
		testButton2.setOpaque(false);
		testButton2.setActionCommand("dice1");
		testButton1.setActionCommand("dice2");
		testButton1.addActionListener(this);
		testButton2.addActionListener(this);
		rollButton.setBorder(null);
		rollButton.setBorderPainted(false);
		rollButton.setContentAreaFilled(false);
		rollButton.setOpaque(false);
		rollButton.setRolloverIcon(new ImageIcon("images/button/roll1.png"));
		endTurn.setBorder(null);
		endTurn.setBorderPainted(false);
		endTurn.setContentAreaFilled(false);
		endTurn.setOpaque(false);
		endTurn.setRolloverIcon(new ImageIcon("images/button/end1.png"));
		rollButton.addActionListener(this);
		rollButton.setActionCommand("roll");
		endTurn.addActionListener(this);
		endTurn.setActionCommand("end");
		buttonRoll.addActionListener(this);
		buttonRoll.setActionCommand("rollDoubles");
		buttonBail.addActionListener(this);
		buttonBail.setActionCommand("bail");
		buttonBuy.addActionListener(this);
		buttonBuy.setActionCommand("buy");
		buttonPay.addActionListener(this);
		buttonPay.setActionCommand("pay");
		buttonOk.addActionListener(this);
		buttonOk.setActionCommand("Ok");
		buttonGo.addActionListener(this);
		buttonGo.setActionCommand("gotojail");
		for(int i = 0; i < 28; i++){
			Deeds[i].addActionListener(this);
			Deeds[i].setActionCommand(("setPI"+i));
		}
		endTurn.setEnabled(false);
		JailButton.setVisible(false);
		buttonBail.setVisible(false);
		buttonRoll.setVisible(false);
		buttonBuy.setVisible(false);
		buttonPay.setVisible(false);
		buttonOk.setVisible(false);
		buttonGo.setVisible(false);
		panel.add(buttonSell);
		panel.add(buttonBuild);
		panel.add(buttonMortgage);
		panel.add(testButton1);
		panel.add(testButton2);
		panel.add(buttonRoll);
		panel.add(buttonBail);
		panel.add(buttonBuy);
		panel.add(buttonPay);
		panel.add(buttonOk);
		panel.add(buttonGo);
		panel.add(JailButton);
		panel.add(start);
		for (int i = 0; i < orderTurn.length; i++){
			player[orderTurn[i]].getToken().getTokenLabel().setBounds(player[orderTurn[i]].getToken().getxLocation(), player[orderTurn[i]].getToken().getyLocation(), 35, 35);
		}
		panel.add(DiceLabel1);
		panel.add(DiceLabel2);
		for (int i = 0; i < orderTurn.length; i++) {
			panel.add(player[orderTurn[i]].getToken().getTokenLabel());
		}
		for (int i = 0; i < 28; i++) {
			panel.add(Deeds[i]);
		}
		panel.add(Pimage);
		panel.add(buttonTrade);
		panel.add(buttonAuction);
		panel.add(buttonBet);
		panel.add(rollButton);
		panel.add(endTurn);
		panel.add(labelTurn1);
		panel.add(labelTurn2);
		panel.add(labelMonopoly);
		this.playerturn();
		
	}

	public void actionPerformed(ActionEvent e) {
		if("dice1".equals(e.getActionCommand())){
			diceValue1 %= 6;
			diceValue1++;
			temp1 %= 6;
			temp1++;
			this.setLabelDice();
		}
		if("dice2".equals(e.getActionCommand())){
			diceValue2 %= 6;
			diceValue2++;
			temp2 %= 6;
			temp2++;
			this.setLabelDice();
		}
		if("roll".equals(e.getActionCommand())){
			rollButton.setEnabled(false);
			setnCounter1(0);
			isRolling = true;
			this.startThread();
			endTurn.setEnabled(false);
			rollButton.setEnabled(false);
			labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));

		}
		if("end".equals(e.getActionCommand())){
			
			endTurn.setEnabled(false);
			labelTurn1.setText(properties.getNameOfProperty(player[orderTurn[turn]].getToken().getLocation()));
			labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
			if((turn+1)==orderTurn.length){
				turn=0;
			}
			else{
				turn++;
			}
			temp = !(player[orderTurn[turn]].isJail());
			if(!temp){
				this.createLabelJail();
			}
			else
				rollButton.setEnabled(true);
			labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
			this.playerturn();
			}
		for(int i=0 ;i <=27;i++ ){
				if(("setPI"+i).equals(e.getActionCommand())){
					labelTurn1.setIcon(new ImageIcon("images/deeds/1."+i+".png"));
				}
				
		}
		if("rollDoubles".equals(e.getActionCommand())){
			setnCounter1(0);
			isRoll = true;
			isRolling = true;
			this.startThread();
			setnCounter1(0);
			this.removeLabelJail();
			rollButton.setEnabled(false);
			endTurn.setEnabled(true);
			labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
		}
		if("bail".equals(e.getActionCommand())){
			player[orderTurn[turn]].setMoney(player[orderTurn[turn]].getMoney() - 50000);
			player[orderTurn[turn]].setJail(false);
			labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
			rollButton.setEnabled(true);
			this.removeLabelJail();
		}
		if("buy".equals(e.getActionCommand())){
			player[orderTurn[turn]].buyProperty(properties.getProperty(player[orderTurn[turn]].getToken().getLocation()));
			this.removeLabelBuy();
			labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
			if(!isPair()){
				endTurn.setEnabled(true);
			}
			else{
				rollButton.setEnabled(true);
			}
		}
		if("pay".equals(e.getActionCommand())){
			player[orderTurn[turn]].payRent(properties.getProperty(player[orderTurn[turn]].getToken().getLocation()), properties.getProperty(player[orderTurn[turn]].getToken().getLocation()).getTitle(player[orderTurn[turn]].getToken().getLocation()).getOwner(), player, orderTurn, Dice1.getValue()+Dice2.getValue());
			this.removeLabelPay();
			labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
			if(!isPair()){
				endTurn.setEnabled(true);
			}
			else{
				rollButton.setEnabled(true);
			}
		}
		if("gotojail".equals(e.getActionCommand())){
			this.removeLabelGoToJail();
			this.setToJail(true);
			labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
			setnCounter1(player[orderTurn[turn]].getToken().getLocation());
			this.startThread();
		}
		if("Ok".equals(e.getActionCommand())){
			this.removeUI();
			this.setMove(this.getMove());
			labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
			if(!isPair()){
				endTurn.setEnabled(true);
			}
			else{
				rollButton.setEnabled(true);
			}
		}
		/*if (player[orderTurn[turn]].getCanBuildOn(i)) {
			buttonBuild.setEnabled(true);
			buttonSell.setEnabled(true);
		}*/
		if("build".equals(e.getActionCommand())){
			//buttonBuild.setEnabled(false);
			this.showBuildButton();
			//rollButton.setEnabled(false);
			//endTurn.setEnabled(false);
		}
		
		if("Sell".equals(e.getActionCommand())){
			this.showSellButton();
		}
		if("start".equals(e.getActionCommand())){
			rollButton.setEnabled(true);
			start.setVisible(false);
			testButton1.setEnabled(true);
			testButton2.setEnabled(true);
		}
		for (int i = 0; i < 22; i++) {
			if(("sell"+i).equals(e.getActionCommand())){
				if(i<=4)
				{	
					if(i==0){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(1).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==1){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(0).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==2){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(3).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(4).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==3){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(2).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(4).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==4){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(2).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(3).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
				House[i].setIcon(new ImageIcon("images/buildings/house"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}else if(i<=10)
				{	if(i==5){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(6).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(7).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==6){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(5).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(7).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==7){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(5).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(6).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==8){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(9).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(10).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==9){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(8).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(10).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==10){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(8).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(9).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
				House[i].setIcon(new ImageIcon("images/buildings/housel"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}else if(i<=16)
				{	if(i==11){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(12).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(13).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==12){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(11).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(13).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==13){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(11).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(12).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==14){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(15).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(16).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==15){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(14).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(16).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==16){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(14).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(15).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
				House[i].setIcon(new ImageIcon("images/buildings/houseu"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}else if(i<=22)
				{	if(i==17){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(18).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(19).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==18){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(17).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(19).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==19){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(18).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(17).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==20){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(21).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
					if(i==21){
						if(properties.getProperty1(i).getEstate().getHouse()>=properties.getProperty1(20).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()-1);
						}
					}
				House[i].setIcon(new ImageIcon("images/buildings/houser"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}
				
				this.resetButtons();
			}
		}
		for (int i = 0; i < 22; i++) {
			if(("build"+i).equals(e.getActionCommand())){
				if(i<=4)
				{	
					if(i==0){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(1).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==1){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(0).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==2){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(3).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(4).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==3){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(2).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(4).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==4){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(2).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(3).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
				House[i].setIcon(new ImageIcon("images/buildings/house"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}else if(i<=10)
				{	if(i==5){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(6).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(7).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==6){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(5).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(7).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==7){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(5).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(6).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==8){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(9).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(10).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==9){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(8).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(10).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==10){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(8).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(9).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
				House[i].setIcon(new ImageIcon("images/buildings/housel"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}else if(i<=16)
				{	if(i==11){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(12).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(13).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==12){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(11).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(13).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==13){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(11).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(12).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==14){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(15).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(16).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==15){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(14).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(16).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==16){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(14).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(15).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
				House[i].setIcon(new ImageIcon("images/buildings/houseu"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}else if(i<=22)
				{	if(i==17){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(18).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(19).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==18){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(17).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(19).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==19){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(18).getEstate().getHouse()&&properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(17).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==20){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(21).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
					if(i==21){
						if(properties.getProperty1(i).getEstate().getHouse()<=properties.getProperty1(20).getEstate().getHouse()){
							properties.getProperty1(i).getEstate().setHouse(properties.getProperty1(i).getEstate().getHouse()+1);
						}
					}
				House[i].setIcon(new ImageIcon("images/buildings/houser"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}
				this.resetButtons();
			}
		}
	}

	public void updateHouse(){
		for (int i = 0; i < 22; i++) {
			if(properties.getProperty1(i).getEstate().getHouse() != 0){
				if(properties.getProperty1(i).getEstate().getHouse() == 1){
					House[i].setText("");
				}
				if(i<=4){
					House[i].setIcon(new ImageIcon("images/buildings/house"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}
				if(i<=10){
					House[i].setIcon(new ImageIcon("images/buildings/housel"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}
				if(i<=16){
					House[i].setIcon(new ImageIcon("images/buildings/houseu"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}
				if(i<=22){
					House[i].setIcon(new ImageIcon("images/buildings/houser"+properties.getProperty1(i).getEstate().getHouse()+".png"));
				}
			}
		}
	}

	public void showBuildButton(){
		for (int i = 0; i < 8; i++) {
			if(player[orderTurn[turn]].getCanBuildOn(i)){
				if(i==0){
					if(properties.getProperty1(0).getEstate().getHouse()<5 && properties.getProperty1(0).getEstate().getHouse()<=properties.getProperty1(1).getEstate().getHouse())
						Build[0].setVisible(true);
					if(properties.getProperty1(1).getEstate().getHouse()<5 && properties.getProperty1(1).getEstate().getHouse()<=properties.getProperty1(0).getEstate().getHouse())
						Build[1].setVisible(true);
				}
				if(i==1){
					if(properties.getProperty1(2).getEstate().getHouse()<5 && properties.getProperty1(2).getEstate().getHouse()<=properties.getProperty1(3).getEstate().getHouse() && properties.getProperty1(2).getEstate().getHouse()<=properties.getProperty1(4).getEstate().getHouse())
						Build[2].setVisible(true);
					if(properties.getProperty1(3).getEstate().getHouse()<5 && properties.getProperty1(3).getEstate().getHouse()<=properties.getProperty1(2).getEstate().getHouse() && properties.getProperty1(3).getEstate().getHouse()<=properties.getProperty1(4).getEstate().getHouse())
						Build[3].setVisible(true);
					if(properties.getProperty1(4).getEstate().getHouse()<5 && properties.getProperty1(4).getEstate().getHouse()<=properties.getProperty1(3).getEstate().getHouse()&&properties.getProperty1(4).getEstate().getHouse()<=properties.getProperty1(2).getEstate().getHouse())
						Build[4].setVisible(true);		
				}
				if(i==2){
					if(properties.getProperty1(5).getEstate().getHouse()<5 && properties.getProperty1(5).getEstate().getHouse()<=properties.getProperty1(6).getEstate().getHouse()&&properties.getProperty1(5).getEstate().getHouse()<=properties.getProperty1(7).getEstate().getHouse())
						Build[5].setVisible(true);
					if(properties.getProperty1(6).getEstate().getHouse()<5 && properties.getProperty1(6).getEstate().getHouse()<=properties.getProperty1(5).getEstate().getHouse()&&properties.getProperty1(6).getEstate().getHouse()<=properties.getProperty1(7).getEstate().getHouse())
						Build[6].setVisible(true);
					if(properties.getProperty1(7).getEstate().getHouse()<5 && properties.getProperty1(7).getEstate().getHouse()<=properties.getProperty1(6).getEstate().getHouse()&&properties.getProperty1(7).getEstate().getHouse()<=properties.getProperty1(5).getEstate().getHouse())
						Build[7].setVisible(true);					
				}
				if(i==3){
					if(properties.getProperty1(8).getEstate().getHouse()<5 && properties.getProperty1(8).getEstate().getHouse()<=properties.getProperty1(9).getEstate().getHouse()&&properties.getProperty1(8).getEstate().getHouse()<=properties.getProperty1(10).getEstate().getHouse())
						Build[8].setVisible(true);
					if(properties.getProperty1(9).getEstate().getHouse()<5 && properties.getProperty1(9).getEstate().getHouse()<=properties.getProperty1(8).getEstate().getHouse()&&properties.getProperty1(9).getEstate().getHouse()<=properties.getProperty1(10).getEstate().getHouse())
						Build[9].setVisible(true);
					if(properties.getProperty1(10).getEstate().getHouse()<5 && properties.getProperty1(10).getEstate().getHouse()<=properties.getProperty1(9).getEstate().getHouse()&&properties.getProperty1(10).getEstate().getHouse()<=properties.getProperty1(8).getEstate().getHouse())
						Build[10].setVisible(true);					
				}
				if(i==4){
					if(properties.getProperty1(11).getEstate().getHouse()<5 && properties.getProperty1(11).getEstate().getHouse()<=properties.getProperty1(12).getEstate().getHouse()&&properties.getProperty1(11).getEstate().getHouse()<=properties.getProperty1(13).getEstate().getHouse())
						Build[11].setVisible(true);
					if(properties.getProperty1(12).getEstate().getHouse()<5 && properties.getProperty1(12).getEstate().getHouse()<=properties.getProperty1(11).getEstate().getHouse()&&properties.getProperty1(12).getEstate().getHouse()<=properties.getProperty1(13).getEstate().getHouse())
						Build[12].setVisible(true);
					if(properties.getProperty1(13).getEstate().getHouse()<5 && properties.getProperty1(13).getEstate().getHouse()<=properties.getProperty1(12).getEstate().getHouse()&&properties.getProperty1(13).getEstate().getHouse()<=properties.getProperty1(11).getEstate().getHouse())
						Build[13].setVisible(true);					
				}
				if(i==5){
					if(properties.getProperty1(14).getEstate().getHouse()<5 && properties.getProperty1(14).getEstate().getHouse()<=properties.getProperty1(15).getEstate().getHouse()&&properties.getProperty1(14).getEstate().getHouse()<=properties.getProperty1(16).getEstate().getHouse())
						Build[14].setVisible(true);
					if(properties.getProperty1(15).getEstate().getHouse()<5 && properties.getProperty1(15).getEstate().getHouse()<=properties.getProperty1(14).getEstate().getHouse()&&properties.getProperty1(15).getEstate().getHouse()<=properties.getProperty1(16).getEstate().getHouse())
						Build[15].setVisible(true);
					if(properties.getProperty1(16).getEstate().getHouse()<5 && properties.getProperty1(16).getEstate().getHouse()<=properties.getProperty1(15).getEstate().getHouse()&&properties.getProperty1(16).getEstate().getHouse()<=properties.getProperty1(14).getEstate().getHouse())
						Build[16].setVisible(true);					
				}
				if(i==6){
					if(properties.getProperty1(17).getEstate().getHouse()<5 && properties.getProperty1(17).getEstate().getHouse()<=properties.getProperty1(18).getEstate().getHouse()&&properties.getProperty1(17).getEstate().getHouse()<=properties.getProperty1(19).getEstate().getHouse())
						Build[17].setVisible(true);
					if(properties.getProperty1(18).getEstate().getHouse()<5 && properties.getProperty1(18).getEstate().getHouse()<=properties.getProperty1(17).getEstate().getHouse()&&properties.getProperty1(18).getEstate().getHouse()<=properties.getProperty1(19).getEstate().getHouse())
						Build[18].setVisible(true);
					if(properties.getProperty1(19).getEstate().getHouse()<5 && properties.getProperty1(19).getEstate().getHouse()<=properties.getProperty1(18).getEstate().getHouse()&&properties.getProperty1(19).getEstate().getHouse()<=properties.getProperty1(17).getEstate().getHouse())
						Build[19].setVisible(true);					
				}
				if(i==7){
					if(properties.getProperty1(20).getEstate().getHouse()<5 && properties.getProperty1(20).getEstate().getHouse()<=properties.getProperty1(21).getEstate().getHouse())
						Build[20].setVisible(true);
					if(properties.getProperty1(21).getEstate().getHouse()<5 && properties.getProperty1(21).getEstate().getHouse()<=properties.getProperty1(20).getEstate().getHouse())
						Build[21].setVisible(true);					
				}
				
			}
		}
	}

	public void resetButtons(){
		for(int i=0;i<22;i++){
			Build[i].setVisible(false);
		}
		for(int i=0;i<22;i++){
			Sell[i].setVisible(false);
		}
	}

	public void showSellButton(){
		for (int i = 0; i < 22; i++) {
			if(player[orderTurn[turn]].getCanSellOn(i)){
				if(i==0){
					if(properties.getProperty1(0).getEstate().getHouse()>0 && properties.getProperty1(0).getEstate().getHouse()>=properties.getProperty1(1).getEstate().getHouse())
						Sell[0].setVisible(true);
					if(properties.getProperty1(1).getEstate().getHouse()>0 && properties.getProperty1(1).getEstate().getHouse()>=properties.getProperty1(0).getEstate().getHouse())
						Sell[1].setVisible(true);
				}
				if(i==1){
					if(properties.getProperty1(2).getEstate().getHouse()>0 && properties.getProperty1(2).getEstate().getHouse()>=properties.getProperty1(3).getEstate().getHouse() && properties.getProperty1(2).getEstate().getHouse()>=properties.getProperty1(4).getEstate().getHouse())
						Sell[2].setVisible(true);
					if(properties.getProperty1(3).getEstate().getHouse()>0 && properties.getProperty1(3).getEstate().getHouse()>=properties.getProperty1(2).getEstate().getHouse() && properties.getProperty1(3).getEstate().getHouse()>=properties.getProperty1(4).getEstate().getHouse())
						Sell[3].setVisible(true);
					if(properties.getProperty1(4).getEstate().getHouse()>0 && properties.getProperty1(4).getEstate().getHouse()>=properties.getProperty1(3).getEstate().getHouse()&&properties.getProperty1(4).getEstate().getHouse()>=properties.getProperty1(2).getEstate().getHouse())
						Sell[4].setVisible(true);		
				}
				if(i==2){
					if(properties.getProperty1(5).getEstate().getHouse()>0 && properties.getProperty1(5).getEstate().getHouse()>=properties.getProperty1(6).getEstate().getHouse()&&properties.getProperty1(5).getEstate().getHouse()>=properties.getProperty1(7).getEstate().getHouse())
						Sell[5].setVisible(true);
					if(properties.getProperty1(6).getEstate().getHouse()>0 && properties.getProperty1(6).getEstate().getHouse()>=properties.getProperty1(5).getEstate().getHouse()&&properties.getProperty1(6).getEstate().getHouse()>=properties.getProperty1(7).getEstate().getHouse())
						Sell[6].setVisible(true);
					if(properties.getProperty1(7).getEstate().getHouse()>0 && properties.getProperty1(7).getEstate().getHouse()>=properties.getProperty1(6).getEstate().getHouse()&&properties.getProperty1(7).getEstate().getHouse()>=properties.getProperty1(5).getEstate().getHouse())
						Sell[7].setVisible(true);					
				}
				if(i==3){
					if(properties.getProperty1(8).getEstate().getHouse()>0 && properties.getProperty1(8).getEstate().getHouse()>=properties.getProperty1(9).getEstate().getHouse()&&properties.getProperty1(8).getEstate().getHouse()>=properties.getProperty1(10).getEstate().getHouse())
						Sell[8].setVisible(true);
					if(properties.getProperty1(9).getEstate().getHouse()>0 && properties.getProperty1(9).getEstate().getHouse()>=properties.getProperty1(8).getEstate().getHouse()&&properties.getProperty1(9).getEstate().getHouse()>=properties.getProperty1(10).getEstate().getHouse())
						Sell[9].setVisible(true);
					if(properties.getProperty1(10).getEstate().getHouse()>0 && properties.getProperty1(10).getEstate().getHouse()>=properties.getProperty1(9).getEstate().getHouse()&&properties.getProperty1(10).getEstate().getHouse()>=properties.getProperty1(8).getEstate().getHouse())
						Sell[10].setVisible(true);					
				}
				if(i==4){
					if(properties.getProperty1(11).getEstate().getHouse()>0 && properties.getProperty1(11).getEstate().getHouse()>=properties.getProperty1(12).getEstate().getHouse()&&properties.getProperty1(11).getEstate().getHouse()>=properties.getProperty1(13).getEstate().getHouse())
						Sell[11].setVisible(true);
					if(properties.getProperty1(12).getEstate().getHouse()>0 && properties.getProperty1(12).getEstate().getHouse()>=properties.getProperty1(11).getEstate().getHouse()&&properties.getProperty1(12).getEstate().getHouse()>=properties.getProperty1(13).getEstate().getHouse())
						Sell[12].setVisible(true);
					if(properties.getProperty1(13).getEstate().getHouse()>0 && properties.getProperty1(13).getEstate().getHouse()>=properties.getProperty1(12).getEstate().getHouse()&&properties.getProperty1(13).getEstate().getHouse()>=properties.getProperty1(11).getEstate().getHouse())
						Sell[13].setVisible(true);					
				}
				if(i==5){
					if(properties.getProperty1(14).getEstate().getHouse()>0 && properties.getProperty1(14).getEstate().getHouse()>=properties.getProperty1(15).getEstate().getHouse()&&properties.getProperty1(14).getEstate().getHouse()>=properties.getProperty1(16).getEstate().getHouse())
						Sell[14].setVisible(true);
					if(properties.getProperty1(15).getEstate().getHouse()>0 && properties.getProperty1(15).getEstate().getHouse()>=properties.getProperty1(14).getEstate().getHouse()&&properties.getProperty1(15).getEstate().getHouse()>=properties.getProperty1(16).getEstate().getHouse())
						Sell[15].setVisible(true);
					if(properties.getProperty1(16).getEstate().getHouse()>0 && properties.getProperty1(16).getEstate().getHouse()>=properties.getProperty1(15).getEstate().getHouse()&&properties.getProperty1(16).getEstate().getHouse()>=properties.getProperty1(14).getEstate().getHouse())
						Sell[16].setVisible(true);					
				}
				if(i==6){
					if(properties.getProperty1(17).getEstate().getHouse()>0 && properties.getProperty1(17).getEstate().getHouse()>=properties.getProperty1(18).getEstate().getHouse()&&properties.getProperty1(17).getEstate().getHouse()>=properties.getProperty1(19).getEstate().getHouse())
						Sell[17].setVisible(true);
					if(properties.getProperty1(18).getEstate().getHouse()>0 && properties.getProperty1(18).getEstate().getHouse()>=properties.getProperty1(17).getEstate().getHouse()&&properties.getProperty1(18).getEstate().getHouse()>=properties.getProperty1(19).getEstate().getHouse())
						Sell[18].setVisible(true);
					if(properties.getProperty1(19).getEstate().getHouse()>0 && properties.getProperty1(19).getEstate().getHouse()>=properties.getProperty1(18).getEstate().getHouse()&&properties.getProperty1(19).getEstate().getHouse()>=properties.getProperty1(17).getEstate().getHouse())
						Sell[19].setVisible(true);					
				}
				if(i==7){
					if(properties.getProperty1(20).getEstate().getHouse()>0 && properties.getProperty1(20).getEstate().getHouse()>=properties.getProperty1(21).getEstate().getHouse())
						Sell[20].setVisible(true);
					if(properties.getProperty1(21).getEstate().getHouse()>0 && properties.getProperty1(21).getEstate().getHouse()>=properties.getProperty1(20).getEstate().getHouse())
						Sell[21].setVisible(true);					
				}
			}
		}
	}
	

	public void createAndShowGUI(){
		this.initComponents();
		frame.add(panel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(5, 5, 1008,734);
		frame.setVisible(true);
	}
	
	public void run() {
		while(isRolling){
			temp1 = generate.nextInt(6)+1;
			temp2 = generate.nextInt(6)+1;
			this.setLabelDice();
			nCounter2++;
			if(nCounter2 == 10){
				isRolling = false;
				if(isRoll){
					isRolling = false;
					this.Roll();
					if(Dice1.getValue() == Dice2.getValue()){
						temp1 = Dice1.getValue();
						temp2 = Dice2.getValue();
						setMove(temp1 + temp2);
						this.setLabelDice();
						player[orderTurn[turn]].setJail(false);
						this.isRoll = false;
					}
					else{
						nCounter2 = 0;
						endTurn.setEnabled(true);
					}
					this.startGame();
				}
				else{
					this.Roll();
					temp1 = Dice1.getValue();
					temp2 = Dice2.getValue();
					setMove(Dice1.getValue() + Dice2.getValue());
					this.setLabelDice();
					nCounter2 = 0;
					this.startGame();
				}
			}
			try
            {
                Thread.sleep(100);
            }
            catch(Exception exception) { }
            DiceLabel1.updateUI();
            DiceLabel2.updateUI();
		}
		while(isMove){
			this.Move();
			setnCounter1(getnCounter1() + 1);
			if(getnCounter1() == getMove()){
				isMove=false;
				checker.CheckTiles(player, orderTurn, turn, properties.getProperty(player[orderTurn[turn]].getToken().getLocation()), this);
			}
			try
            {
                Thread.sleep(300);
            }
            catch(Exception exception) { }
		}
		while(isToJail()){
			if(player[orderTurn[turn]].getToken().getLocation() > 10){
				this.unMove();
				setnCounter1(getnCounter1() - 1);
			}
			else{
				this.Move();
				setnCounter1(getnCounter1() + 1);
			}
			if(getnCounter1() == 10){
				setToJail(false);
				endTurn.setEnabled(true);
			}
			try
            {
                Thread.sleep(300);
            }
            catch(Exception exception) { }
		}
		while(isForward()){
			this.Move();
			setnCounter1(getnCounter1() + 1);
			if(player[orderTurn[turn]].getToken().getLocation() == getMove()){
				setForward(false);
				this.setButtons();
				checker.CheckTiles(player, orderTurn, turn, properties.getProperty(player[orderTurn[turn]].getToken().getLocation()), this);
			}
			try
            {
                Thread.sleep(300);
            }
            catch(Exception exception) { }
		}
		while(isBackward()){
			this.unMove();
			setnCounter1(getnCounter1() - 1);
			if(getnCounter1() == getMove()){
				setBackward(false);
				this.setButtons();
				checker.CheckTiles(player, orderTurn, turn, properties.getProperty(player[orderTurn[turn]].getToken().getLocation()), this);
			}
			try
            {
                Thread.sleep(300);
            }
            catch(Exception exception) { }
		}
	}
	
	public void Move(){
		player[orderTurn[turn]].getToken().moveLocation(1);
		player[orderTurn[turn]].getToken().moveToken();
		player[orderTurn[turn]].getToken().getTokenLabel().setLocation(player[orderTurn[turn]].getToken().getxLocation(),player[orderTurn[turn]].getToken().getyLocation());
		if(player[orderTurn[turn]].getToken().getLocation() == 0){
			player[orderTurn[turn]].setMoney(player[orderTurn[turn]].getMoney()+2000000);
		}
		labelTurn1.setIcon(Block[player[orderTurn[turn]].getToken().getLocation()]);
		labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
	}
	
	private void unMove() {
		player[orderTurn[turn]].getToken().unmoveToken();
		player[orderTurn[turn]].getToken().moveLocation(-1);
		player[orderTurn[turn]].getToken().getTokenLabel().setLocation(player[orderTurn[turn]].getToken().getxLocation(),player[orderTurn[turn]].getToken().getyLocation());
		labelTurn1.setText(properties.getNameOfProperty(player[orderTurn[turn]].getToken().getLocation()));
		labelTurn2.setText(String.valueOf(player[orderTurn[turn]].getMoney()));
	}

	public void Roll(){
		Dice1.Roll();
		Dice2.Roll();
		//Dice1.setValue(diceValue1);
		//Dice2.setValue(diceValue2);
		//Dice1.setValue(0);
		//Dice2.setValue(1);
	}
	
	public void startGame(){
		temp = !(player[orderTurn[turn]].isJail());
		this.isPair();
		if(temp){
			if(player[orderTurn[turn]].isJail()){
				temp = false;
				this.setToJail(true);
				setnCounter1(player[orderTurn[turn]].getToken().getLocation());
				this.setPair(false);
				this.player[orderTurn[turn]].setPair(0);
			}
			else{
				isMove = true;
				setMove(Dice1.getValue() + Dice2.getValue());
			}
		}
		else{
			jailCounter++;
			if(jailCounter == 3){
				player[orderTurn[turn]].setJail(false);
				player[orderTurn[turn]].getToken().setGoToJail(false);
				this.player[orderTurn[turn]].setPair(0);
				jailCounter = 0;
			}
		}
	}
				
	public void setLabelDice(){
		if(temp1 == 1){DiceLabel1.setIcon(iiDice1);}
		if(temp1 == 2){DiceLabel1.setIcon(iiDice2);}
		if(temp1 == 3){DiceLabel1.setIcon(iiDice3);}
		if(temp1 == 4){DiceLabel1.setIcon(iiDice4);}
		if(temp1 == 5){DiceLabel1.setIcon(iiDice5);}
		if(temp1 == 6){DiceLabel1.setIcon(iiDice6);}
		if(temp2 == 1){DiceLabel2.setIcon(iiDice1);}
		if(temp2 == 2){DiceLabel2.setIcon(iiDice2);}
		if(temp2 == 3){DiceLabel2.setIcon(iiDice3);}
		if(temp2 == 4){DiceLabel2.setIcon(iiDice4);}
		if(temp2 == 5){DiceLabel2.setIcon(iiDice5);}
		if(temp2 == 6){DiceLabel2.setIcon(iiDice6);}
	}
	
	public void updateButtons(){
		if(!isPair()){
			endTurn.setEnabled(true);
		}
		else{
			rollButton.setEnabled(true);
		}
	}
	
	public void checkPair(){
		if(Dice1.getValue() == Dice2.getValue()){
			this.setPair(true);
			this.player[orderTurn[turn]].setPair(player[orderTurn[turn]].getPair()+1);
			if(player[orderTurn[turn]].getPair() == 5){
				player[orderTurn[turn]].setJail(true);
			}
		}
		else{
			this.player[orderTurn[turn]].setPair(0);
			this.setPair(false);
		}
	}

	public void setPair(boolean isPair) {
		this.isPair = isPair;
	}

	public boolean isPair() {
		this.checkPair();
		return isPair;
	}
	
	public void setForward(boolean isForward) {
		this.isForward = isForward;
	}

	public boolean isForward() {
		return isForward;
	}

	public void setBackward(boolean isBackward) {
		this.isBackward = isBackward;
	}

	public boolean isBackward() {
		return isBackward;
	}

	public void setMove(int move) {
		Move = move;
	}

	public int getMove() {
		return Move;
	}

	public void setToJail(boolean isToJail) {
		this.isToJail = isToJail;
	}

	public boolean isToJail() {
		return isToJail;
	}

	public void setnCounter1(int nCounter1) {
		this.nCounter1 = nCounter1;
	}

	public int getnCounter1() {
		return nCounter1;
	}
	
	public void playerturn() {
		Pimage.setIcon(player[orderTurn[turn]].getToken().getTokenLabel().getIcon());
	}
}
